//
//  AXHdzqMsVC.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/10/30.
//  Copyright © 2018 tzhg. All rights reserved.
//

#import "AXHdzqMsVC.h"
#import "Header.h"
#import "ZXCategories.h"
#import "AXHdzqMsZzfqVC.h"
#import "AXHdzqMsJjkqVC.h"
#import "AXHdzqMsMrygVC.h"
#import "UIView+SDAutoLayout.h"
#import "AXHdzqMsZzfqVC.h"
#import "AXHdzqMsJjkqVC.h"
#import "AXHdzqMsMrygVC.h"


#define Start_X          10.0f      // 第一个按钮的X坐标
#define Start_Y          7.0f     // 第一个按钮的Y坐标
#define Width_Space      50.0f      // 2个按钮之间的横间距
#define Height_Space     10.0f     // 竖间距
#define Button_Height   30.0f    // 高
#define Button_Width     (SCREEN_WIDTH-120)/3   // 宽

@interface AXHdzqMsVC ()<UIScrollViewDelegate>
/** topView */
@property (nonatomic , strong) UIView *topView;
@property (nonatomic , strong) UIButton *shopButton;
@property  (nonatomic , strong) UIScrollView *scrollView;
@property (nonatomic , strong) AXHdzqMsZzfqVC *vc1;
@property (nonatomic , strong) AXHdzqMsJjkqVC *vc2;
@property (nonatomic , strong) AXHdzqMsMrygVC *vc3;


@end

@implementation AXHdzqMsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupNavigationItems];
    
    [self craetView];
    [self addButton];
  
    
}



- (void)craetView {
    
    self.topView = [[UIView alloc]init];
    self.topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.topView];
  
    [self setlayout];
    
    /** 创建 */
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.topView.frame)+10, SCREEN_WIDTH, SCREEN_HEIGHT - CGRectGetMaxY(self.topView.frame)-10)];
    self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH *3, 0);
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    

    self.vc1 = [[AXHdzqMsZzfqVC alloc]init];
    _vc1.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, _scrollView.mj_h);
    [self addChildViewController:_vc1];
    
    self.vc2 = [[AXHdzqMsJjkqVC alloc]init];
    _vc2.view.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, _scrollView.mj_h);
    [self addChildViewController:_vc2];
    
    self.vc3 = [[AXHdzqMsMrygVC alloc]init];
    _vc3.view.frame = CGRectMake(SCREEN_WIDTH*2, 0, SCREEN_WIDTH, _scrollView.mj_h);
    [self addChildViewController:_vc3];
    
    
    [_scrollView addSubview:_vc1.view];
    [_scrollView addSubview:_vc2.view];
    [_scrollView addSubview:_vc3.view];
    
    
}

- (void)setlayout {
    
    self.topView.sd_layout
    .leftSpaceToView(self.view, 0)
    .topSpaceToView(self.view, 0)
    .rightSpaceToView(self.view, 0)
    .heightIs(44);
    [self.view layoutIfNeeded];
    
    
    
}

#pragma mark - action

- (void)addButton {
    
    
    NSArray *arr = @[@"正在疯抢",@"即将开抢",@"明日预告"];
 
    for (int i = 0 ; i < 3; i++) {
        NSInteger index = i % 3;
        NSInteger page = i / 3;
        
        // 圆角按钮
        self.shopButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.shopButton setTitleColor:COLOR_DIV forState:UIControlStateNormal];
        [self.shopButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        self.shopButton.backgroundColor = [UIColor colorWithHex:0xFFE6E6E6];
        self.shopButton.cornerRadius = 5;
        [self.shopButton setTitle:arr[i] forState:UIControlStateNormal];
        self.shopButton.tag = 100 + i;//这句话不写等于废了
        self.shopButton.frame = CGRectMake(index * (Button_Width + Width_Space) + Start_X, page  * (Button_Height + Height_Space)+Start_Y, Button_Width, Button_Height);
        [self.topView addSubview:self.shopButton];
        //按钮点击方法
        [self.shopButton addTarget:self action:@selector(mapBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        if (i == 0) {
            [self mapBtnClick:_shopButton];
        }
    }
    
}

/** 三个按钮的点击方法 */
- (void)mapBtnClick:(UIButton *)sender{
   
    sender.selected = YES;
    sender.backgroundColor = COLOR_GREEN;
    for (NSInteger i = 0; i < 3; i++) {
        UIButton *button = [_topView viewWithTag:100 + i];
        if (button.tag != sender.tag) {
            button.selected = NO;
            button.backgroundColor = [UIColor colorWithHex:0xFFE6E6E6];
        }
    }
    
    [_scrollView setContentOffset:CGPointMake(SCREEN_WIDTH * (sender.tag - 100), 0) animated:YES];
    
}









#pragma mark - Action




- (void)setupNavigationItems {
    
    /** 导航条左边的按钮 */
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    backBtn.frame = CGRectMake(0, 0, 30, 30);
    [backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    //右边按钮
    UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [shareBtn setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    shareBtn.frame = CGRectMake(0, 0, 60, 30);
    [shareBtn addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:shareBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    /** title */
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    titleLabel.text = @"秒杀";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = FONT_EIGHTEEN;
    titleLabel.textColor = [UIColor colorWithHex:0x29b566];
    self.navigationItem.titleView = titleLabel;
    
}

- (void)backAction:(UIBarButtonItem *)item {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)shareAction:(UIBarButtonItem *)item {
    
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    NSInteger a = scrollView.contentOffset.x / SCREEN_WIDTH;
    UIButton *button = [_topView viewWithTag:a + 100];
    [self mapBtnClick:button];
    
}




@end

